<template>
  <div>
    <div class="back">
      <el-form   class="form"
        label-position="left"  :model="form"
        :rules="rules"  ref="form">
        <p class="title">Welcome</p>
        <el-form-item prop="username">
          <el-input  v-model="form.username"
            placeholder="请输入管理员账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input  v-model="form.password"
            type="password" placeholder="请输入管理员密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="onSubmit()">登录后台管理系统</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请填写用户名", trigger: "blur" },
          {
            pattern: /^\w{3,15}$/,
            message: "3~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请填写用户名", trigger: "blur" },
          {
            pattern: /^\w{6,15}$/,
            message: "6~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    onSubmit() {
      this.$refs['form'].validate(valid=>{
        if(valid){
           console.log(this.form);

           this.$http.adminApi.login(this.form).then(res=>{
            console.log('登录请求结果',res);
            if(res.data.code==200){
              this.$store.commit('updateName',res.data.data.user)
              this.$store.commit('saveToken',res.data.data.token)
              
              this.$router.push('/home/index')
              this.$message({type:'success',message:'登录成功'})
              
            }else{
              this.$message({type:'error',message:`登录失败:${res.data.msg}`})
            }
            
           })
        }else{
          this.$message({type:'danger',message:'验证失败,请修改表单'})
        }
      })
    },
  },
};
</script>

<style scoped>
.form {
  display: block;
  width: 20%;
  position: absolute;
  top: 30%;
  left: 40%;
  box-shadow: 0 2px 2px rgba(0,0,0,.2);
}
.form .title {
  text-align: center;
  font-size: 1.5em;
  color: white;
  margin-bottom: 20px;
}
.back {
  height: 100vh;
  background-image: linear-gradient(125deg, #aff4e0, #d2f6c1, #edf7aa,#dcf6b9);
  background-size: 500%;
  animation: animate 20s infinite;
}
@keyframes animate {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
</style>
